const dbApi = require('../models/db')
const jwt = require('../utils/jwt')
const { uuid } = require('../config/config.default')
const handTime = require('../middleware/handtimestamp')

//

// 获取文章内容
exports.ArtilceInfo = async (req, res, next) => {
  const { articleId } = req.params
  let token = req.headers.authorization
  token = token ? token.split('Bearer ')[1] : null
  let concern = false // 是否关注
  let articleSelf = false // 文章是否属于当前用户
  let is_praise = false // 是否已点赞
  let is_collect = false // 是否已收藏
  try {
    // 处理请求
    const sql = `SELECT a.id,a.title,a.content,b.nickname,b.username,b.avatar,a.time FROM
     articles a  INNER JOIN users b 
    ON a.username = b.username WHERE a.id = ${articleId}`
    const [data] = await dbApi.query(sql)
    if (token) {
      //   //已登录
      const userInfo = await jwt.verify(token, uuid)
      //   //判断当前文章信息是否属于当前登陆用户
      if (userInfo.uid !== data.username) {
        // 不等于才查询 是否已关注
        const fansssql = `SELECT * FROM fans WHERE follower ='${userInfo.uid}' AND be_follower ='${data.username}'`
        const fans = await dbApi.query(fansssql)
        concern = fans.length > 0
      } else {
        articleSelf = true
      }

      //   // //是否已点赞
      const praisesql = `SELECT *FROM artuserpraise 
      WHERE username = '${userInfo.uid}' AND praise_articleid = ${articleId}`
      const praise = await dbApi.query(praisesql)
      is_praise = praise.length > 0
      //   // //是否已收藏
      const collectssql = `SELECT * FROM artusercollect 
       WHERE username = '${userInfo.uid}' AND collect_articleid = ${articleId}`
      const collect = await dbApi.query(collectssql)
      is_collect = collect.length > 0
    }
    //   //文章评论总数
    const countsql = `SELECT COUNT(*) as commentcount FROM usercomment WHERE article_id = ${articleId}`
    const count = await dbApi.query(countsql)
    const commentcount = count.length > 0 ? count[0].commentcount : 0
    data.is_followed = concern // 关注
    data.belongSelf = articleSelf // 文章属于自己吗
    data.is_praise = is_praise // 是否点赞
    data.is_collect = is_collect // 是否收藏
    data.commentcount = commentcount // 文章评论总数
    res.status(201).json(data)
  } catch (error) {
    next(error)
  }
}

// 文章点赞
exports.addArticlePraise = async (req, res, next) => {
  try {
    // 处理请求
    const { articleId } = req.body
    const sql = `INSERT INTO artuserpraise (praise_articleid,username)
    VALUES(${articleId},'${req.user.username}')`
    const { affectedRows: data } = await dbApi.query(sql)
    data
      ? res.status(201).json({
        message: 'succeed'
      })
      : res.status(400).json({
        message: 'Invalid request'
      })
  } catch (error) {
    next(error)
  }
}

// 取消点赞
exports.delArticlePraise = async (req, res, next) => {
  try {
    // 处理请求
    const { articleId } = req.body
    const sql = `DELETE FROM artuserpraise 
    WHERE username ='${req.user.username}' AND praise_articleid =${articleId}`
    const { affectedRows: data } = await dbApi.query(sql)
    data
      ? res.status(201).json({
        message: 'succeed'
      })
      : res.status(400).json({
        message: 'Invalid request'
      })
  } catch (error) {
    next(error)
  }
}

// 文章收藏
exports.addArticleCollection = async (req, res, next) => {
  try {
    // 处理请求
    const { articleId } = req.body
    const sql = `INSERT INTO artusercollect (collect_articleid,username)
    VALUES(${articleId},'${req.user.username}')`
    const { affectedRows: data } = await dbApi.query(sql)
    data
      ? res.status(201).json({
        message: 'succeed'
      })
      : res.status(400).json({
        message: 'Invalid request'
      })
  } catch (error) {
    next(error)
  }
}

// 取消收藏
exports.delArticleCollection = async (req, res, next) => {
  try {
    // 处理请求
    const { articleId } = req.body
    const sql = `DELETE FROM artusercollect 
    WHERE username ='${req.user.username}' AND collect_articleid =${articleId}`
    const { affectedRows: data } = await dbApi.query(sql)
    data
      ? res.status(201).json({
        message: 'succeed'
      })
      : res.status(400).json({
        message: 'Invalid request'
      })
  } catch (error) {
    next(error)
  }
}

// //获取某人全部文章
// exports.authorArtilces = async (req, res, next) => {
//   try {
//     //处理请求

//     res.send('1')
//   } catch (error) {
//     next(error)
//   }
// }

// //创建文章
// exports.createAritcls = async (req, res, next) => {
//   let { title, content, cover, channel_id } = req.body
//   const { draft } = req.query //是否存为草稿
//   let status = draft ? 1 : 0 //true 则为草稿
//   const { user } = req
//   cover = JSON.stringify(cover)
//   const sql = `INSERT INTO articles (title,content,username,status,commentstatus,commentcount,channel_id,cover)
//   VALUES ('${title}','${content}','${user.username}',${status},1,0,${channel_id},'${cover}') `
//   try {
//     //处理请求
//     const { affectedRows: data } = await dbApi.query(sql)
//     data
//       ? res.status(201).json({
//           message: 'succeed'
//         })
//       : res.status(400).json({
//           message: 'Invalid request'
//         })
//   } catch (error) {
//     next(error)
//   }
// }

// //更新文章
// exports.setArtilce = async (req, res, next) => {
//   const { id } = req.params
//   const { draft } = req.query
//   let status = draft ? 1 : 0 //true 则为草稿
//   const { title, content, cover, channel_id } = req.body
//   const sql = `UPDATE articles SET title = '${title}',content = '${content}',
//   cover = '${cover}',channel_id=${channel_id} WHERE id = ${id} `
//   try {
//     //处理请求
//     const { affectedRows: data } = await dbApi.query(sql)
//     data
//       ? res.status(201).json({
//           message: 'succeed'
//         })
//       : res.status(400).json({
//           message: 'Invalid request'
//         })
//   } catch (error) {
//     next(error)
//   }
// }

// //删除文章
// exports.delArtilce = async (req, res, next) => {
//   const { id } = req.params
//   const sql = `DELETE FROM articles WHERE id = ${id}`
//   try {
//     //处理请求
//     const { affectedRows: data } = await dbApi.query(sql)
//     data
//       ? res.status(201).json({
//           message: 'success'
//         })
//       : res.status(400).json({
//           message: 'Invalid id'
//         })
//   } catch (error) {
//     next(error)
//   }
// }

// //修改文章评论状态
// exports.setCommentStatus = async (req, res, next) => {
//   const { article_id } = req.query
//   let { allow_comment } = req.body
//   allow_comment = allow_comment ? 1 : 0
//   const sql = `UPDATE articles SET commentstatus =${allow_comment} WHERE id = ${article_id} `
//   try {
//     //处理请求
//     const { affectedRows: data } = await dbApi.query(sql)
//     data
//       ? res.status(201).json({
//           message: 'succeed'
//         })
//       : res.status(400).json({
//           message: 'Invalid request'
//         })
//   } catch (error) {
//     next(error)
//   }
// }

// //添加文章评论
// exports.addcomment = async (req, res, next) => {
//   try {
//     //处理请求

//     res.send('1')
//   } catch (error) {
//     next(error)
//   }
// }

// //删除文章评论
// exports.delComment = async (req, res, next) => {
//   try {
//     //处理请求

//     res.send('1')
//   } catch (error) {
//     next(error)
//   }
// }

// //获取频道列表
// exports.s = async (req, res, next) => {
//   const sql = ` SELECT * FROM channel `
//   try {
//     //处理请求
//     const channels = await dbApi.query(sql)
//     res.status(201).json({
//       channels
//     })
//   } catch (error) {
//     next(error)
//   }
// }
